<template>
    <div id="AllClassNotice">
        <div id="headTitle">班级通知</div>
        <div id="CreateAllClassNotice">
            <el-input id="searchTextInput" v-model="searchTitle" placeholder="请输入搜索的标题"/>
            <el-button type="primary" @click="loadData">搜索</el-button>
        </div>
        <div id="table">
            <el-table max-height="700" :data="allClassNotice" border>
                <el-table-column type="expand">
                    <template #default="props">
                        <div class="tableRowContainer">
                            <p class="tableRow">标题：{{ props.row.noticeTitle }}</p>
                            <p class="tableRow">发布者：{{ props.row.userName }}</p>
                            <p class="tableRow">班级：{{ props.row.className }}</p>
                            <p class="tableRow">发布时间：{{ props.row.releaseTime }}</p>
                            <p class="tableRow">内容：{{ props.row.noticeContent }}</p>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="noticeTitle" label="标题" show-overflow-tooltip align="center"/>
                <el-table-column prop="userName" label="发布者" show-overflow-tooltip width="200" align="center"/>
                <el-table-column prop="className" label="班级" show-overflow-tooltip width="200" align="center"/>
                <el-table-column prop="releaseTime" label="发布时间" show-overflow-tooltip align="center" width="200"/>
                <el-table-column label="操作" width="150" align="center">
                    <template #default="scope">
                        <el-button type="primary" plain @click="deleteClassNotice(scope.row.claNoticeID)">删除通知
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div id="pagination">
            <el-pagination
                    v-model:current-page="pageNumber"
                    v-model:page-size="pageSize"
                    :page-sizes="[5, 10, 20, 50]"
                    :small=false
                    :disabled=false
                    :background=false
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="allClassNoticeTotal"
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
            />
        </div>
    </div>
</template>

<script>
//请求
import request from '@/utils/requset.js';
//顶部提示信息
import prompt from '@/utils/prompt.js'
//路由跳转
import router from "@/router";

export default {
    name: 'allClassNotice',
    data() {
        return {
            pageNumber: 1,
            pageSize: 5,
            searchTitle: "",
            allClassNotice: [],
            allClassNoticeTotal: 0,
        }
    },
    created() {
        this.loadData();
    },
    methods: {
        //加载通知信息
        loadData() {
            let token = localStorage.getItem("token") ? localStorage.getItem("token") : "";
            if (token === "") {
                prompt("还未登录！", "error");
                router.push({path: "/backLogin"});
                return;
            }
            request.get("/classNotice/info", {
                params: {
                    token: token,
                    pageNumber: this.pageNumber,
                    pageSize: this.pageSize,
                    searchTitle: this.searchTitle,
                }
            }).then(result => {
                console.log(result)
                //请求失败
                if (result.code !== "200") {
                    prompt(result.message, result.type);
                }
                //请求成功
                this.allClassNotice = result.data.allClassNotice;
                this.allClassNoticeTotal = result.data.allClassNoticeTotal;
            })
        },
        //删除班级通知
        deleteClassNotice(claNoticeID) {
            let token = localStorage.getItem("token") ? localStorage.getItem("token") : "";
            request.delete(
                "/classNotice/info", {
                    params: {
                        token: token,
                        claNoticeID: claNoticeID,
                    }
                }
            ).then(result => {
                prompt(result.message, result.type);
                //加载新数据
                this.loadData();
            });
        },
        //分页-单页数量
        handleSizeChange(pageSize) {
            this.pageSize = pageSize;
            this.loadData();
        },
        //分页-页码
        handleCurrentChange(pageNumber) {
            this.pageNumber = pageNumber;
            this.loadData();
        },
    }
}
</script>
<style>
#AllClassNotice #headTitle {
    text-align: center;
    font-size: 45px;
    color: #63a1b2;
    margin: 25px auto;
}

#AllClassNotice #CreateAllClassNotice {
    margin: 15px 0;
}

#AllClassNotice #CreateAllClassNotice .el-input {
    width: 400px;
    margin-right: 10px;
}

#AllClassNotice #table {
    border-radius: 5px;
    overflow: hidden;
    margin-top: 25px;
}

#AllClassNotice .tableRowContainer {
    margin: 0 500px;
}

#AllClassNotice .tableRow {
    font-size: 15px;
    padding: 20px;
    margin: 5px 0;
    border: 1px solid #63a1b2;
    border-radius: 10px;
}

#AllClassNotice #pagination {
    position: absolute;
    bottom: 1vh;
}
</style>